
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

import { INewCateGory } from 'src/types/new_category.interface';
import {IAllKindAreaData} from 'src/types/allkindareadata.interface'

type UnionType = INewCateGory | INewCateGory[];


@Injectable()
export class NewCategoryService {

    /**
     * 参数装饰器,NewCategoryTable 这个名字要和app.module.ts中的MongooseModule.forFeature 定义的名字一样
     * NewCategoryTable 是定义好的
     * @param recommendModel 
     */
    constructor(@InjectModel('NewCategoryTable') private newCategoryModel: Model<INewCateGory>) {

    }


    /**
     * 查询数据
     * @param queryCondition 
     * @returns 
     */
    async findService(queryCondition?:object): Promise<UnionType> {
        return this.newCategoryModel.find(queryCondition).exec();
    }


    /***
     * 向mongodb中插入数据
    */
    async importData(requestParamsBody: UnionType): Promise<UnionType> {
        //如果请求体是一个数组
        if (Array.isArray(requestParamsBody)) {
            return this.newCategoryModel.insertMany(requestParamsBody);
        }
        else {
            return new this.newCategoryModel(requestParamsBody).save();
        }
    }


    /**
     * queryCondition = allKindListIdList =[
            "66238848d5f4c2ae75760486", // "手机专区" 4
            "66238848d5f4c2ae75760489", // "电脑专区" 2
            "66238848d5f4c2ae75760488", // "平板专区" 2
            "66238848d5f4c2ae75760487", // "穿戴专区" 2
            "66238848d5f4c2ae7576048b", // "音频专区" 2
    ];
     * @param queryCondition 
     * @returns 
     */
    async queryAllKindAreaById(queryConditionList?:Array<String>) {

      //查询表中哪些字段
      const querySelectFileds = ["categoryParentId","categoryName","productInfo"];

     
        const allData = Promise.all([
            //查询手机专区数据
            this.newCategoryModel.find().where('categoryParentId').equals(queryConditionList[0]).select(querySelectFileds),

            //查询电脑专区数据
            this.newCategoryModel.find().where('categoryParentId').equals(queryConditionList[1]).select(querySelectFileds),

            //查询平板专区数据
            this.newCategoryModel.find().where('categoryParentId').equals(queryConditionList[2]).select(querySelectFileds),

            //查询穿戴专区数据
            this.newCategoryModel.find().where('categoryParentId').equals(queryConditionList[3]).select(querySelectFileds),

            //查询音频专区数据
            this.newCategoryModel.find().where('categoryParentId').equals(queryConditionList[4]).select(querySelectFileds),

        ]);
        return allData;
    }

   
    async getAllProductInfo(page:number,limit:number):Promise<INewCateGory[]>{
        console.log("page====",page)

        const skip = (page-1)* limit;  //类似[].slice(i*limit,(i+1)*limit) 数据的获取
        return this.newCategoryModel.find()
        .sort({createdAt:-1})  //按照创建时间倒序排序
        .skip(skip) //跳过指定的条数
        .limit(limit)
        .exec()
        
    }

 
    async fetchProductListByCategoryId(categoryParentId:string):Promise<UnionType> {

        console.log('categoryParentId====',categoryParentId)
        const querySelectFileds = ["categoryParentId","categoryName","productInfo"];

        const data = this.newCategoryModel.find().where('categoryParentId').equals(categoryParentId).select(querySelectFileds).exec();

        console.log(`根据${categoryParentId}查询到的数据=`,data)
        return data;

    }
   




}



